package com.jt;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

@EnableFeignClients //启动feign方式的服务调用
@SpringBootApplication
public class        ScaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScaConsumerApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    /*
    * 整合了负载均衡的对象
    * */
    @Bean
    @LoadBalanced
    public RestTemplate loadBalancedRestTemplate1(){
        return new RestTemplate();
    }
    @Service
    public class ConsumerService{
        @SentinelResource("doGetResource")
        public String doGetResource(){
            return "doGetResource";
        }
    }
    @RestController
    public class ConsumerController{

        @Value("${spring.application.name}")
        private String appName;
        @Autowired
        private RestTemplate restTemplate;
        @Autowired
        private LoadBalancerClient loadBalancerClient;
        @Autowired
        private RestTemplate loadBalancedRestTemplate1;
        @Autowired
        private ConsumerService consumerService;
        @GetMapping("/consumer/dofindById")
        @SentinelResource("id")
        public String dofindById(@RequestParam("id") Integer id){
            return "resource id is " + id;
        }
        /*基于RestTemplate进行远程服务调用,但是不具备负载均衡特性
        * 不需要nacos,直接将IP写死*/
        private AtomicLong atomicLong = new AtomicLong(1);
        @GetMapping("/consumer/doRestEcho1")
        public String doRestEcho01() throws InterruptedException {
            long l = atomicLong.decrementAndGet();
            if (l%2 == 0)
                Thread. sleep(200);
            consumerService.doGetResource();
            String url = "http://localhost:8081/provider/echo/"+appName;
            System.out.println("request url:"+url);
            return restTemplate.getForObject(url, String.class);
        }
        @GetMapping("/consumer/doRestEcho2")
        public String doRestEcho02(){
            ServiceInstance choose = loadBalancerClient.choose("sca-provider");
            String ip = choose.getHost();
            int port = choose.getPort();
            String url = String.format("http://%s:%s/provider/echo/%S",ip,port,appName);
            System.out.println("request url:"+url);
            return restTemplate.getForObject(url, String.class);
        }
        @GetMapping("/consumer/doRestEcho3")
        public String doRestEcho03(){
            String url = String.format("http://%s/provider/echo/%S", "sca-provider", appName);
            return loadBalancedRestTemplate1.getForObject(url, String.class);
        }
    }

}
